<template>
  <div class="permissions-container">
    <div class="app-container">
      <el-card>
        <el-button type="primary" size="small" @click="handleOpenAddDialog(1,'0')">添加权限</el-button>
        <el-table border stripe :data="permissionTreeData" row-key="id" default-expand-all>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <!-- 作用域插槽 -->
            <template #default="{row}">
              <el-button v-if="row.type===1" type="text" @click="handleOpenAddDialog(2,row.id)">添加权限点</el-button>
              <el-button type="text" @click=" handleOpenEditDialog(row.id)">编辑</el-button>
              <el-button type="text" @click="handleDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!--新增 弹框 -->
      <el-dialog title="新增权限" :visible="showAddDialog" @close="handleAddClose">

        <el-form :data="permForm">
          <el-form-item label="权限名称">
            <el-input v-model="permForm.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="permForm.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="permForm.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permForm.enVisible"
              active-color="#409EFF"
              inactive-color="#ccc"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleAddClose">取消</el-button>
          <el-button @click="handleAdd">确定</el-button>

        </template>
      </el-dialog>
      <!-- 编辑弹框 -->
      <el-dialog title="查看编辑" :visible="showEditDialog" @close="handleEditClose">

        <el-form :data="permForm">
          <el-form-item label="权限名称">
            <el-input v-model="permForm.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="permForm.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="permForm.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <!-- 开关组件 -->
            <el-switch
              v-model="permForm.enVisible"
              active-color="#409EFF"
              inactive-color="#ccc"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="handleEditClose">取消</el-button>
          <el-button @click="handleEdit">确定</el-button>

        </template>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, addFormAPI, getPermissionDetailListAPI, editPermissionDetailAPI, delPermAPI } from '@/api/permissions'
import { list2Tree } from '@/utils/switch'

export default {
  name: 'PermissionsView',
  data() {
    return {
      // 权限树形数据
      permissionTreeData: [],
      // 添加权限显示的弹窗
      showAddDialog: false,
      // 编辑弹框显示
      showEditDialog: false,
      // 权限表单
      permForm: {
        name: '',
        code: '',
        description: '',
        enVisible: '1' }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表
    async getPermissionList() {
      const resp = await getPermissionListAPI()
      console.log(resp)
      // 把列表转为树型
      this.permissionTreeData = list2Tree(resp.data, '0')
    },
    // 添加权限弹框关闭
    handleAddClose() {
      this.showAddDialog = false
    },
    // 处理编辑弹框关闭
    handleEditClose() {
      this.showEditDialog = false
    },

    // 处理新增权限弹框打开时
    handleOpenAddDialog(type, pid) {
      // type:1-->页面访问权，2-->按钮操作权
      // pid:父级id,'0'表示页面访问权，否则就是按钮
      // 保存type和pid给handleAdd用(因为this是共享的，是同一个组件)
      this.type = type
      this.pid = pid
      // 显示弹框
      this.showAddDialog = true
    },
    // 新增权限
    async handleAdd() {
      // 发起新增请求
      const res = await addFormAPI({ ...this.permForm, type: this.type, pid: this.pid })

      console.log(res)
      this.$message.success('操作成功')
      // 关闭弹窗
      this.handleAddClose()
      // 获取权限列表
      this.getPermissionList()
    },
    // 编辑权限
    async handleEdit() {
      try {
        // 点击编辑时，发起编辑请求
        await editPermissionDetailAPI(this.permForm)
        // 关闭弹框
        this.handleEditClose()
        // 获取权限列表
        this.getPermissionList()
      } catch (error) {
        this.$message.error(this.error || '操作失败')
      }
    },
    // 编辑权限弹框打开时
    async  handleOpenEditDialog(id) {
      this.showEditDialog = true
      const resp = await getPermissionDetailListAPI(id)
      this.permForm = resp.data
    },
    // 删除权限
    async handleDel(id) {
      try {
        await delPermAPI(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (err) {
      //
      }
    }
  }

}
</script>

  <style>

  </style>

